package com.daigoudao.web.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.daigoudao.web.dao.ICanBuyDao;
import com.daigoudao.web.dao.IInviteDao;
import com.daigoudao.web.dao.ISimpleBuyDao;
import com.daigoudao.web.dao.IWantBuyDao;
import com.daigoudao.web.model.CanBuyModel;
import com.daigoudao.web.model.InviteModel;
import com.daigoudao.web.model.SimpleBuyModel;
import com.daigoudao.web.model.WantBuyModel;
import com.daigoudao.web.service.IInviteService;

public class InviteServiceImpl implements IInviteService {

	private IInviteDao inviteDao;

	private ISimpleBuyDao simpleBuyDao;

	private ICanBuyDao canBuyDao;

	private IWantBuyDao wantBuyDao;

	@Override
	public void addInviteInfo(int wantId, CanBuyModel canBuyModel)
			throws Exception {
		// 数据库中没有该代购信息，新增一条
		if (canBuyModel.getCanId() == 0) {
			canBuyDao.addCanBuy(canBuyModel);
			if (canBuyModel.getImages() != null && canBuyModel.getImages().size() > 0) {
				canBuyDao.insertImages(canBuyModel);
			}
		} else {
			canBuyModel = canBuyDao.queryCanBuyDetail(canBuyModel.getCanId());
		}

		List<InviteModel> inviteModels = new ArrayList<InviteModel>();
		// 查询当前求购信息，并判断该求购信息是否是发布状态
		WantBuyModel wantBuyModel = wantBuyDao.queryWantBuyDetail(wantId);
		if (wantBuyModel != null && wantBuyModel.getStatus() == 0) {
			InviteModel inviteModel = new InviteModel();
			inviteModel.setCanId(canBuyModel.getCanId());
			inviteModel.setCanUserId(canBuyModel.getUser().getUserid());
			inviteModel.setPrice(canBuyModel.getPrice());
			inviteModel.setWantId(wantId);
			inviteModel.setWantUserId(wantBuyModel.getUser().getUserid());
			inviteModels.add(inviteModel);
		}

		// 查询发布状态的搭车求购信息
		SimpleBuyModel queryModel = new SimpleBuyModel();
		queryModel.getWantBuyModel().setWantId(wantId);
		queryModel.setStatus(0);
		List<SimpleBuyModel> simpleBuyModels = simpleBuyDao
				.querySimpleBuys(queryModel);
		for (SimpleBuyModel simpleBuyModel : simpleBuyModels) {
			InviteModel inviteModel = new InviteModel();
			inviteModel.setCanId(canBuyModel.getCanId());
			inviteModel.setCanUserId(canBuyModel.getUser().getUserid());
			inviteModel.setPrice(canBuyModel.getPrice());
			inviteModel.setWantId(wantId);
			inviteModel.setSimpleId(simpleBuyModel.getSimpleId());
			inviteModel.setWantUserId(simpleBuyModel.getUser().getUserid());
			inviteModels.add(inviteModel);
		}
		if (inviteModels.size() > 0) {
			inviteDao.addInviteInfos(inviteModels);
		}

	}

	/**
	 * 更新邀约表的状态
	 * 
	 * @param inviteModel
	 * @throws Exception
	 */
	public void updateStatus(InviteModel inviteModel) throws Exception {
		inviteDao.updateStatus(inviteModel);
	}

	public List<InviteModel> queryInviteInfos(InviteModel inviteModel)
			throws Exception {
		return inviteDao.queryInviteInfos(inviteModel);
	}

	public int queryCountInvites(InviteModel inviteModel) throws Exception {
		List<InviteModel> inviteModels = inviteDao
				.queryInviteInfos(inviteModel);
		int count = 0;
		for (InviteModel im : inviteModels) {
			if (im.getSelectMode() == inviteModel.getSelectMode()) {
				count++;
			}
		}
		return count;
	}

	public void setInviteDao(IInviteDao inviteDao) {
		this.inviteDao = inviteDao;
	}

	public void setCanBuyDao(ICanBuyDao canBuyDao) {
		this.canBuyDao = canBuyDao;
	}

	public void setWantBuyDao(IWantBuyDao wantBuyDao) {
		this.wantBuyDao = wantBuyDao;
	}

	public ISimpleBuyDao getSimpleBuyDao() {
		return simpleBuyDao;
	}

	public void setSimpleBuyDao(ISimpleBuyDao simpleBuyDao) {
		this.simpleBuyDao = simpleBuyDao;
	}

	@Override
	public void deleteInvit(int invitId) throws Exception {
		inviteDao.deleteInvit(invitId);

	}

	@Override
	public int getInviteCount(InviteModel inviteModel) throws Exception {
		return inviteDao.getInviteCount(inviteModel);
	}

	public InviteModel selectInvite(int inviteId) throws Exception {
		return inviteDao.selectInvite(inviteId);
	}
}
